package com.lanqiao.guimei.controller;

import com.lanqiao.guimei.dao.TradeInfoDao;
import com.lanqiao.guimei.vo.CartItem;
import com.lanqiao.guimei.vo.Page;
import com.lanqiao.guimei.vo.TradeDetail;
import com.lanqiao.guimei.vo.TradeInfo;
import com.lanqiao.web.mvc.annotation.Controller;
import com.lanqiao.web.mvc.annotation.RequestMapping;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 韩原
 * @date 2019/8/14  11:43
 */
@Controller
public class TradeInfoController {

    @RequestMapping("/Trdif/page")
    public String findForPage(HttpServletRequest request, HttpSession session, TradeInfo tradeInfo){
        String time1 = request.getParameter("time1");
        System.out.println(time1);
        String time2 =request.getParameter("time2");
        System.out.println(time2);
        //创建了一个session作用域 将page的对象先存进去
        Page<TradeInfo> trdifPage = (Page<TradeInfo>) session.getAttribute("trdifPage");
        //创建了一个UsersDao对象
        TradeInfoDao tradeInfoDao = new TradeInfoDao();

        String pageNo = request.getParameter("pageNo");

        if (trdifPage == null || StringUtils.isBlank(pageNo)){
            trdifPage = new Page<TradeInfo>();
            //得到每页显示的记录数
            trdifPage.setPageSize(8);
            //得到当前页号
            trdifPage.setPageNo(1);
            //设置查询分页的条件
            trdifPage.setQueryObject(tradeInfo);
            // 统计总记录数
            trdifPage.setTotalRow(tradeInfoDao.countForPage(trdifPage,time1,time2));

        } else {
            //得到当前页号
            trdifPage.setPageNo(Integer.valueOf(pageNo));
        }
        System.out.println(trdifPage);
        session.setAttribute("trdifPage", trdifPage);

        // 查询列表数据
        List<TradeInfo> trdifList = tradeInfoDao.findForPage(trdifPage,time1,time2);
        System.out.println(trdifList.size());
        session.setAttribute("trdifList", trdifList);
        return "/after/orderquery.jsp";
    }

    @RequestMapping("/Trdif/update")
    public Map trdifupdata(TradeInfo tradeInfo,HttpServletRequest request){
        Boolean b=new TradeInfoDao().trdupdata(tradeInfo);
      if (b) {
          Map json = new HashMap();
          json.put("result", b);
          return json;
      }else {
          Map json = new HashMap();
          json.put("result", b);

      }
         return null;
    }

    @RequestMapping("/Trdif/delete")
    public Map trdifdelete(TradeInfo tradeInfo,HttpServletRequest request){
        Boolean b=new TradeInfoDao().trddelete(tradeInfo);
        if (b) {
            Map json = new HashMap();
            json.put("result", b);
            return json;
        }else {
            Map json = new HashMap();
            json.put("result", b);
            return json;
        }
    }

   @RequestMapping("/Trdif/finddetail")
    public String trdiffinddetail(TradeInfo tradeInfo,HttpServletRequest request) {
       //查询列表数据
       List<TradeDetail> detailList = new TradeInfoDao().trdfind(tradeInfo);
       System.out.println(detailList);
       request.setAttribute("detailList", detailList);
       return "/after/orderdetails.jsp";
   }


    /**
     * 拿到填写订单的数据，把信息封装到tradeDetail 里。成功跳转到结算页面
     * @param tradeInfo
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/front/tra/order")
    public String traOrder(TradeInfo tradeInfo,HttpServletRequest request,HttpSession session){
       /* String buyer = request.getParameter("buyer");
        String totalMoney = request.getParameter("totalMoney");
        String receiver = request.getParameter("receiver");
        String phone = request.getParameter("phone");
        String trdLoc = request.getParameter("trdLoc");*/

        Map<String, CartItem> cartItemMap = (Map<String, CartItem>) session.getAttribute("cart");
        Map<String, Integer> salerIdMap = (Map<String, Integer>) session.getAttribute("salerIdMap");
        List<TradeDetail> tradeDetails= new ArrayList<>();
        /* Integer salerId = (Integer)session.getAttribute("saler");*/
        for (CartItem item : cartItemMap.values()) {
            String prodNo = item.getProduct().getProdNo();
            TradeDetail tradeDetail = new TradeDetail(prodNo, salerIdMap.get(prodNo), item.getQuantity(),item.getSubPrice());
            tradeDetails.add(tradeDetail);
        }
        String s = new TradeInfoDao().addTrade(tradeInfo, tradeDetails);
        if (s !=null){
            session.removeAttribute("cart");
            session.removeAttribute("salerIdMap");
            //跳去结算的页面
            return "redirect:/front/success.jsp";
        } else {
            return "redirect:/front/lose.jsp";
        }
    }

}
